package com.ys.web.user.collection;

import com.ys.utils.base.http.ResponseHandler;
import com.ys.utils.base.j2se.StringHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/collection/")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;

    /**
     * 收藏
     *
     * @param uid
     * @param collection
     * @return
     */
    @RequestMapping("save")
    public String save(@RequestAttribute long uid, Collection collection) {
        collection.setUid(uid);
        collectionService.saveCollection(collection);
        return ResponseHandler.success();
    }

    @RequestMapping("deleteById")
    public String deleteById(Collection collection) {
        if (StringHandler.isEmpty(collection.getId())) {
            return ResponseHandler.error("500", "参数传递错误");
        }
        collectionService.deleteById(collection);
        return ResponseHandler.success();
    }

    /**
     * 收藏列表
     *
     * @param uid
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("list")
    public String list(@RequestAttribute long uid, int pageNumber, int pageSize, String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("pageNumber", (pageNumber - 1) * pageSize);
        map.put("pageSize", pageSize);
        map.put("type", type);
        return ResponseHandler.list(collectionService.getCollectionListPage(map));
    }
}
